﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Diagnostics;

[assembly: WebResource("AjaxControlToolkit.WebControlLibrary.Images.update.gif", "img/gif")]
[assembly: WebResource("AjaxControlToolkit.WebControlLibrary.Images.updateh.gif", "img/gif")]
[assembly: WebResource("AjaxControlToolkit.WebControlLibrary.TextBox.js", "text/javascript")]

namespace AjaxControlToolkitCustom.TextEditor
{
	/// <summary>
	/// Summary description for HtmlEditor
	/// </summary>
	[ValidationProperty("Text")]
	public class HtmlEditor : CompositeControl, IScriptControl
	{
		protected string text = String.Empty;
		protected HiddenField htmlencodedText;
		protected HiddenField modified;
		protected HtmlGenericControl htmlEditor;
		protected HtmlGenericControl designEditor;
		protected HtmlGenericControl htmlArea;
		protected HtmlGenericControl designArea;
		protected HtmlGenericControl toolbarContainer;
		protected HtmlGenericControl htmlTab;
		protected HtmlGenericControl designTab;
		public Button btnEd;
		public TextBox txtEd;
		public Label lblEd;
		public ImageButton imgbtnEd;
		protected UpdatePanel updatePanel;
		protected bool updateHtml = false;
		protected ScriptManager sm;

		public string txtEditorID
		{
			get
			{
				if (ViewState["txtEditorID"] != null)
					return (string)ViewState["txtEditorID"];
				else
					return String.Empty;
			}
			set { ViewState["txtEditorID"] = value; }
		}

		public string imgbtnEdID
		{
			get
			{
				if (ViewState["imgbtnEdID"] != null)
					return (string)ViewState["imgbtnEdID"];
				else
					return String.Empty;
			}
			set { ViewState["imgbtnEdID"] = value; }
		}

		public string lblEdID
		{
			get
			{
				if (ViewState["lblEdID"] != null)
					return (string)ViewState["lblEdID"];
				else
					return String.Empty;
			}
			set { ViewState["lblEdID"] = value; }
		}

		public string HoverImageUrl
		{
			get
			{
				object value = ViewState["hoverImageUrl"];

				return (value == null) ? string.Empty : (string)value;
			}
			set
			{
				ViewState["hoverImageUrl"] = value;
			}
		}

		public string OutImageUrl
		{
			get
			{
				object value = ViewState["outImageUrl"];

				return (value == null) ? string.Empty : (string)value;
			}
			set
			{
				ViewState["outImageUrl"] = value;
			}
		}


		protected string GetWebResourceUrl(string resource)
		{
			return Page.ClientScript.GetWebResourceUrl(this.GetType(), resource);
		}

		protected HtmlGenericControl CreateEditorArea()
		{
			HtmlGenericControl area = new HtmlGenericControl("div");
			area.ID = "divID";
			area.Style["width"] = "140px";
			area.Style["height"] = "60px";
			area.Style["background-color"] = "green";

			lblEd = new Label();
			lblEd.Style["width"] = "140px";
			lblEd.Style["height"] = "15px";
			lblEd.Style["background-color"] = "pink";
			lblEd.Style["display"] = "none";
			lblEd.Text = "Test";

			imgbtnEd = new ImageButton();
			string resource = "AjaxControlToolkit.WebControlLibrary.Images.update.gif";
			imgbtnEd.ImageUrl = GetWebResourceUrl(resource);

			txtEd = new TextBox();

			HtmlImage corner = new HtmlImage();
			//string resource = "AjaxControlToolkit.Winthusiasm.HtmlEditor.Images.pixel.gif";
			//corner.Src = GetWebResourceUrl(resource);
			corner.Height = 1;
			corner.Width = 1;
			corner.Alt = "";
			corner.Style["position"] = "absolute";
			corner.Style["margin"] = "0px";
			corner.Style["padding"] = "0px";
			corner.Style["background-color"] = "red";
			corner.Style["font-size"] = "0px";
			
			area.Controls.Add(txtEd);
			area.Controls.Add(lblEd);
			area.Controls.Add(imgbtnEd);

			return area;
		}


		protected override void CreateChildControls()
		{
			this.Controls.Add(CreateEditorArea());

			base.CreateChildControls();
		}

		protected override void OnPreRender(EventArgs e)
		{
			SetIDs();

			sm = ScriptManager.GetCurrent(Page);
			if (sm == null)
				throw new HttpException("A ScriptManager control must exist on the current page.");
			sm.RegisterScriptControl(this);

			base.OnPreRender(e);
		}

		protected void SetIDs()
		{
			txtEditorID = "xEditor";
			txtEd.ID = txtEditorID;
			lblEdID = "lblEdX";
			lblEd.ID = lblEdID;
			imgbtnEdID = "imgBtnEdX";
			imgbtnEd.ID = imgbtnEdID;
			string resourceHover = "AjaxControlToolkit.WebControlLibrary.Images.updateh.gif";
			HoverImageUrl = GetWebResourceUrl(resourceHover);
			string resourceOut = "AjaxControlToolkit.WebControlLibrary.Images.update.gif";
			OutImageUrl = GetWebResourceUrl(resourceOut);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			sm.RegisterScriptDescriptors(this);

			base.Render(writer);
		}

		protected virtual IEnumerable<ScriptReference> GetScriptReferences()
		{
			ScriptReference htmlEditorReference = new ScriptReference("AjaxControlToolkit.WebControlLibrary.TextBox.js", "AjaxControlToolkit");

			return new ScriptReference[] { htmlEditorReference };
		}

		protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
		{
			ScriptControlDescriptor descriptor = new ScriptControlDescriptor("AjaxControlToolkit.TextEditor.HtmlEditor", this.ClientID);
			descriptor.AddProperty("txtEditorID", this.txtEd.ClientID);
			descriptor.AddProperty("lblEdID", this.lblEd.ClientID);
			descriptor.AddProperty("imgbtnEdID", this.imgbtnEd.ClientID);
			descriptor.AddProperty("hoverImageUrl", HoverImageUrl);
			descriptor.AddProperty("outImageUrl", OutImageUrl);
			
			return new ScriptDescriptor[] { descriptor };
		}

		#region IScriptControl Members

		IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
		{
			return GetScriptDescriptors();
		}

		IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
		{
			return GetScriptReferences();
		}

		#endregion


	}
}
